package com.mingzhi.controller;

import com.mingzhi.pojo.Dept;
import com.mingzhi.pojo.Result;
import com.mingzhi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制类
 *
 * @RestController 作用详细描述：
 * RestController是Controller的子类
 * 用于定义控制器，并且直接返回json格式的数据
 */
@RestController
public class DeptController {

    // 注入DeptService
    @Autowired
    private DeptService deptService;

    /**
     * 查询全部部门列表
     *
     * @return
     */
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list() {
        System.out.println("查询全部部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 根据 deptId 删除部门
     *
     * @param deptId
     * @return
     */
    /*@DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        System.out.println("根据id删除部门");
        deptService.deleteById(id);
        return Result.success();
    }*/
    @DeleteMapping("/depts")
    public Result delete(@RequestParam("id") Integer deptId) {
        System.out.println("根据ID删除部门: " + deptId);
        deptService.deleteById(deptId);
        return Result.success();
    }
    /**
     * @DeleteMapping("/depts")
     * public Result delete(HttpServletRequest request){
     *     String idStr = request.getParameter("id");
     *     int id = Integer.parseInt(idStr);
     *     System.out.println("根据ID删除部门: " + id);
     *     return Result.success();
     * }
     */

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
        System.out.println("新增部门");
        System.out.println(dept);
        deptService.add(dept);
        return Result.success();
    }
}
